package dataModelBuilder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import util.write2File;

import dao.*;
import dao.mysql.impl.ProductDao;
import dao.mysql.impl.ProductParamsDao;
import dao.mysql.impl.ReviewDao;
import dataModel.Product;
import dataModel.Review;

public class ProductBuilder {
	IProductDao productDao = new ProductDao();
	IProductParamsDao paramsDao = new ProductParamsDao();
	IReviewDao reviewDao = new ReviewDao();

	public void build(List<Product> products) {
		productDao.selectAll(products);

		Map<Integer, Map<String, String>> params = new HashMap<Integer, Map<String, String>>();
		paramsDao.selectAll(params);

		Map<Integer, List<Review>> reviews = new HashMap<Integer, List<Review>>();
		reviewDao.selectAll(reviews);

		for (Product prod : products) {
			int id = prod.getId();
			prod.setParams(params.get(id));
			prod.setReviews(reviews.get(id));
		}
	}

	public static void main(String[] args) {
		ProductBuilder builder = new ProductBuilder();
		List<Product> products = new ArrayList<Product>();
		builder.build(products);

		for (Product prod : products) {
			write2File.writeReview(
					prod.getReviews(),
					util.Configuration.REVIEW_INIT
							+ prod.getTitle().replaceAll("手机", "")
									.replaceAll(" ", "") + ".txt");

			System.out
					.println(prod.getTitle()
							+ "\t"
							+ (prod.getParams() != null ? prod.getParams()
									.size() : 0)
							+ "\t"
							+ (prod.getReviews() != null ? prod.getReviews()
									.size() : 0) + "\t" + prod.getParamString()
							+ "\t" + prod.getReviewString());
		}
	}
}
